﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CSharp.Libs.Koc;

namespace CSharp.Libs.WPF.UserControls
{
    /// <summary>
    /// Interaktionslogik für KocInformantControl.xaml
    /// </summary>
    public partial class KocInformantControl : UserControl
    {
        #region declarations
        private KocInformantControlViewModel _KICVM = null;
        private Kernel _KRNL = null;
        #endregion



        #region constructor / finalizer
        public KocInformantControl()
        {
            InitializeComponent();
        }
        #endregion



        #region event handling
        private void lvUserServerEntries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //let only 2 selcted Items be possible
            if (lvUserServerEntries.SelectedItems.Count == 2)
            {
                //get items
                TblInformationItem tii1 = lvUserServerEntries.SelectedItems[0] as TblInformationItem;
                TblInformationItem tii2 = lvUserServerEntries.SelectedItems[1] as TblInformationItem;

                //make sure item 2 is the older one
                if (tii1.PrevTick > tii2.PrevTick)
                {
                    TblInformationItem tiiTemp = tii1;
                    tii1 = tii2;
                    tii2 = tiiTemp;
                }

                //Calculate diffs.
                Int64 intPrevTickDiff = tii2.PrevTick - tii1.PrevTick;
                Int64 intGoldDiff = tii2.SumGold - tii1.SumGold;
                Int64 intFoodDiff = tii2.SumFood - tii1.SumFood;
                Int64 intWoodDiff = tii2.SumWood - tii1.SumWood;
                Int64 intStoneDiff = tii2.SumStone - tii1.SumStone;
                Int64 intOreDiff = tii2.SumOre - tii1.SumOre;
                Int64 intAetherstoneDiff = tii2.SumAetherstone - tii1.SumAetherstone;

                //write to fields
                lblDiffPrevTick.Content = intPrevTickDiff.ToString("#,###");
                lblDiffGold.Content = intGoldDiff.ToString("#,###");
                lblDiffFood.Content = intFoodDiff.ToString("#,###");
                lblDiffWood.Content = intWoodDiff.ToString("#,###");
                lblDiffStone.Content = intStoneDiff.ToString("#,###");
                lblDiffOre.Content = intOreDiff.ToString("#,###");
                lblDiffAetherstone.Content = intAetherstoneDiff.ToString("#,###");
            }
            else
            {
                //clear fields
                lblDiffPrevTick.Content = "";
                lblDiffGold.Content = "";
                lblDiffFood.Content = "";
                lblDiffWood.Content = "";
                lblDiffStone.Content = "";
                lblDiffOre.Content = "";
                lblDiffAetherstone.Content = "";
            }
        }
        #endregion



        #region private functions
        private void _InitializeVM(
            Kernel KRNL)
        {
            _KRNL = KRNL;
            _KICVM = new KocInformantControlViewModel(this.Dispatcher, _KRNL);
            DataContext = _KICVM;
        }
        #endregion



        #region interface
        #region properties
        /// <summary>
        /// 
        /// </summary>
        /// <history>created  12.06.2012</history>
        public KocInformantControlViewModel KICVM { get { return _KICVM; } }

        /// <summary>
        /// 
        /// </summary>
        /// <history>created  10.07.2012</history>
        public KocInformantControlViewModel ViewModel { get { return _KICVM; } }
        #endregion

        #region methods
        public void InitializeVM(
            Kernel KRNL)
        {
            _InitializeVM(KRNL);
        }
        #endregion
        #endregion
    }
}
